package goldeneagle.com.user.servlet;

import com.alibaba.fastjson.JSONObject;
import goldeneagle.com.entity.User;
import goldeneagle.com.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *Created by GHW
 *Description:用户注册
 *2019/11/12 22:35
 */
@WebServlet(value = "/UserRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("pwd");
        String userType = "user";
        String img = "././././web/images/default.png";
        String address = request.getParameter("address");
        String email = request.getParameter("email");

        //userType只能是user
        //默认头像
        if (true) {  //判断两次密码是否一致（前端），后端再判断一次
            User user = new User(username, password, address, email, userType, img);
            UserService userService = new UserService();
            userService.UserRegister(user);
            response.getWriter().write("ok");
            response.getWriter().write("[id:" + user.getId() + ",username:" + user.getUsername() + ",userType:"
                    + user.getUserType() + "]");
            response.sendRedirect("login.jsp");
        } else {
            response.getWriter().write("failed");
            System.out.println("信息错误");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
